package com.yourannet.android.commons;

import com.yourannet.android.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 警告弹出框
 * @author GuannanYan
 *
 */
public class Alert extends Activity implements OnClickListener{ 
             
	public final static String OK ="确定";
	public final static String CANCEL="取消"; 
	private final static Alert alert=new Alert();
	static AlertDialog.Builder alertDialog=null;  
	PopupWindow popup=null;
	LayoutInflater inFlater=null;      
	Button btnClose=null;
	TextView lblTitle=null;   
	RadioButton rdoYes=null;   
	RadioButton rdoNo=null;
	ImageView imageLine3=null;
	View dialogView=null;
	int m_width=LinearLayout.LayoutParams.WRAP_CONTENT;
	int m_height=LinearLayout.LayoutParams.WRAP_CONTENT;
	boolean m_isShow=false;
    public View getDialogView(){
    	return dialogView;
    }
	//"setPositiveButton"、"setNegativeButton" 或 "setNeutralButton" 函式都可以用来定义按钮，各按钮分别预设代表正面/中立/负面的结果。
	public Alert(){
		//android.R.attr.windowTitleStyle
	} 
	public Alert(Activity activity,int width,int height){
		instance(activity,width,height,null);
	}   
	
	public Alert(Activity activity,int width,int height,Drawable dra){
		instance(activity,width,height,dra);
	}
	
	public Alert(String msg,String title){}
	
	/**
	 * 
	 * @param activity  当前活动窗体
	 * @param width 对话框宽
	 * @param height 对话框高
	 * @param resId 对话框布局 文件
	 */
	public Alert(Activity activity,int width,int height,int resId){
		this.inFlater=activity.getLayoutInflater();
		dialogView=inFlater.inflate(resId, null,true);
		popup=new PopupWindow(dialogView,width,height);
		popup.setFocusable(true);
	}
	
	private static boolean isProgressShow = false;
	public static boolean isProgressShow(){
		return isProgressShow;
	}
	static View progressView=null;
	static PopupWindow progressPopup=null;
	/**
	 * 模式加载条
	 * @param activity 当前activity
	 * @param msg 加载提示消息
	 */
	public static void progresShow(Activity activity,String msg){
		LayoutInflater layoutInflater=activity.getLayoutInflater();
		progressView=layoutInflater.inflate(R.layout.progress_layout, null,false);
		TextView txtMsg=(TextView) progressView.findViewById(R.id.txt_msg_progress);
		txtMsg.setText(msg);
		txtMsg.requestFocus();
		txtMsg.setFocusable(true);
		progressPopup=new PopupWindow(progressView,-1,-1);
		progressPopup.setAnimationStyle(R.anim.fade);
		progressPopup.showAtLocation(progressView, Gravity.CENTER, 0, 0);
		isProgressShow = true;
	}
	/**
	 * 
	 *<p>弹出正在加载提示框</p>
	 *
	 *@creation 2012-6-28
	 *
	 *@author guannanYan
	 *
	 *@param msg提示消息
	 *@param sender消息触发者
	 */
	public static void progresShow(String msg,View sender){
		progresShow((Activity)sender.getContext(),msg);  
		mSender=sender;
		mSender.setEnabled(false);
	}
	
	static View mSender=null;
	/**
	 * 
	 *<p>关闭正在加载提示框</p>
	 *
	 *@creation 2012-6-28
	 *
	 *@author guannanYan
	 *
	 */
	public static void progressClose(){
		if(null!=progressPopup){
			progressPopup.dismiss();
			progressPopup=null;
		}
		if(null!=mSender){
			mSender.setEnabled(true);
			mSender=null;
		}
		
		isProgressShow = false;
	}
	
	/*public Alert(Activity activity,int drawableResId,int animResId,int width,int height){
		this.inFlater=activity.getLayoutInflater();
		dialogView=inFlater.inflate(drawableResId, null,false);
		if(width<0||height<0){
			popup=new PopupWindow(dialogView,m_width,m_height);
		}else{
			popup=new  PopupWindow(dialogView,width,height);
		}
		popup=new PopupWindow(dialogView,m_width,m_height);
		popup.setAnimationStyle(animResId);
		popup.setFocusable(true);
		popup.update();
	}
	smaple:
	Alert alert=new Alert(MapActivity.this,R.drawable.edit_mark_info,R.anim.in,-1,-1);
		alert.show();
		Toast.makeText(this, m_markPosition_info.getText()+"", 0).show();
	*/
	
	public void show(){
		popup.showAtLocation(dialogView, Gravity.CENTER, 0, 0);
		m_isShow=true;
	}
	public void close(){
			closeConfirm();
	}
	
	/**
	 * 
	 * @param activity  当前上下文
	 * @param width  宽度
	 * @param height 高度
	 */
	private void instance(Activity activity,int width,int height,Drawable dra){
		this.inFlater=activity.getLayoutInflater();
		dialogView=inFlater.inflate(R.drawable.confirm_dialog, null,false);
		/*if(dra != null){
			dialogView.setBackgroundDrawable(dra);
		}*/

		popup=new PopupWindow(dialogView,width,height);
		lblTitle= (TextView) dialogView.findViewById(R.id.dialog_title);
		rdoYes=(RadioButton)dialogView.findViewById(R.id.rdo_yes);
		rdoNo=(RadioButton) dialogView.findViewById(R.id.rdo_no);
		imageLine3 = (ImageView)dialogView.findViewById(R.id.img_separate3);
		btnClose= (Button) dialogView.findViewById(R.id.img_btn_close_dialog);
	    btnClose.setOnClickListener(this);
	}
	
	
	
	DialogInterface.OnClickListener  alertHander= new DialogInterface.OnClickListener(){

		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			
		}
		
	};
//	/**
//	 * 一个按钮的
//	 * @param args 当前activity
//	 * @param msg  对话框显示信息
//	 */
//	public static void show(String msg){
//		AlertDialog alzter=new AlertDialog.Builder(alert).setMessage(msg).setNegativeButton(Alert.OK, new DialogInterface.OnClickListener(){
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//				// TODO Auto-generated method stub
//				
//			}}).show();
//	}
	/**两个按钮的弹出框
	 * which -1  确定  -2 中立  -3  取消
	 * @param args  当前activity
	 * @param msg   对话框显示信息
	 * @param title 对话框标题
	 * @param click 按钮事件
	 */
	public static void confirm(Context context,String msg,String title,DialogInterface.OnClickListener click){
		alertDialog=new AlertDialog.Builder(context);
		alertDialog.setTitle(title).setIcon(R.drawable.icon_option).setMessage(msg).setPositiveButton(Alert.OK,click).setNeutralButton(Alert.CANCEL,click).show();
	}
	public static void confirm(Context context,String msg,DialogInterface.OnClickListener click){
		alertDialog=new AlertDialog.Builder(context);
		alertDialog.setMessage(msg).setPositiveButton(Alert.OK,click).setNeutralButton(Alert.CANCEL,click).show();
	}
	/**
	 * 确认对话框
	 * @param title  标题
	 * @param yesText yes按钮 显示文字  
	 * @param noText no按钮显示文字
	 */
	public  void confirm(String title,String yesText,String noText,int styleRes){
		lblTitle.setText(title);
		rdoYes.setText(yesText);
		if(noText.equals(""))
			rdoNo.setVisibility(View.GONE);
		else
			rdoNo.setText(noText);
		popup.setAnimationStyle(styleRes);
	    popup.showAtLocation(dialogView, Gravity.CENTER, 0, 0);
	    m_isShow=true;
	}
	
	public  void confirm(String title,String yesText,String noText){
		lblTitle.setText(title);
		rdoYes.setText(yesText);
		rdoNo.setText(noText);
		rdoNo.setChecked(false);
	    popup.showAtLocation(dialogView, Gravity.CENTER, 0, 0);
	    m_isShow=true;
	}
	/**
	 * 关闭确认对话框
	 */
	public void  closeConfirm(){
		if(popup!=null&&m_isShow){
			popup.dismiss();
		}
	}

	public void onClick(View v) {
		closeConfirm();
	}
	/**
	 * 一个按钮的弹出框
	 * @param args 当前activity
	 * @param msg  对话框显示信息
	 * @param title对话框标题
	 * @param click按钮事件
	 */
	public static void show(Context context,String msg,String title,DialogInterface.OnClickListener click){
		alertDialog=new AlertDialog.Builder(context);
		alertDialog.setTitle(title).setIcon(R.drawable.icon_option).setMessage(msg).setPositiveButton(Alert.OK,click).show();
	}
	public static void show(Context context,String msg,DialogInterface.OnClickListener click){
		alertDialog=new AlertDialog.Builder(context);
		alertDialog.setMessage(msg).setPositiveButton(Alert.OK,click).show();
	}
	public static void show(Context context,String msg,String title){
		alertDialog=new AlertDialog.Builder(context);
		alertDialog.setMessage(msg).setTitle(title).setIcon(R.drawable.icon_option).show();
	}
	public static void show(Context context,String msg){
		alertDialog=new AlertDialog.Builder(context);
		alertDialog.setMessage(msg).show();
	}
	

	/**
	 * 
	 * @param args 当前activity
	 * @param msg  提示框显示信息
	 */
	public static void popup(Context args,String msg){
		Toast.makeText(args, msg, Toast.LENGTH_SHORT).show(); 
	}
	/**
	 * 
	 * @param args  当前activity
	 * @param msg   提示框显示信息
	 * @param state 提示框显示时间  1：较长，0：较短
	 */
	public static void popup(Context args,String msg,int state){
		if(state==1){
			Toast.makeText(args, msg,Toast.LENGTH_LONG).show();
		}else{
			Toast.makeText(args, msg,Toast.LENGTH_SHORT).show();
		}		
	}
	
	/**
	 * 加载更多视图
	 * @param activity
	 * @return
	 */
	public static View progressMore(Activity activity){
		return activity.getLayoutInflater().inflate(R.layout.progress_layout, null,false);
	}
	public static void emptyToast(Activity activity){
		Toast toast=new Toast(activity);
		toast.setView(activity.getLayoutInflater().inflate(R.layout.empty_toast, null,false));
		toast.show();	
	}
	
	
	

}

